package com.sicheng.lc.周赛.分类.递归;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/22 11:20
 */
public class 计算字符串的数字和 {
    //https://leetcode.cn/problems/calculate-digit-sum-of-a-string/
    public String digitSum(String s, int k) {
        if (s.length() == k)
            return s;
        int last = 0;
        StringBuilder stack = new StringBuilder();
        int x = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i - last >= k) {
                stack.append(x);
                last = i;
                x = 0;
            }
            x = x + s.charAt(i) - '0';
        }
        stack.append(x);

        String res = stack.toString();
        if (stack.length() > k) {
            res = digitSum(res, k);
        }
        return res;
    }

    public static void main(String[] args) {
        计算字符串的数字和 solution = new 计算字符串的数字和();
        String s = solution.digitSum("11111222223", 3);
        System.out.println(s);
    }
}
